﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WXJH
{
    [Description("XmlHelper XML文件操作类")]
    public  class XmlHelper
    {
        /// <summary>
        /// 读取filePath文件的Node节点数据
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [Description("读取filePath文件的Node节点数据")]
        public static List<string> ReadXml(string filePath,string nodeName)
        {
            List<string> strnodes = new List<string>();          
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);
            //根节点root
            XmlNode rootNode = xDoc.SelectSingleNode("root");
            string name = rootNode.Name;
            if (rootNode != null)
            {
                //获取根节点下面的子节点
                XmlNodeList nodeList = rootNode.ChildNodes;
                //遍历子节点，查找名称的子节点
                foreach (XmlNode item in nodeList)
                {
                    if (item.Name == nodeName)
                    {
                        //获取孙子节点
                        XmlNodeList subNodeList = item.ChildNodes;
                        if (subNodeList != null)
                        {
                            foreach (XmlNode xn in subNodeList)
                            {
                                XmlElement xmle = (XmlElement)xn;
                                strnodes.Add(xmle.InnerText);
                            }
                        }
                        return strnodes;
                    }
                }
            }
            return null;
        }
        /// <summary>
        /// 读取filePath文件的Node节点下的第一个数据
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [Description("读取filePath文件的Node节点下的第一个数据")]
        public static string ReadXmlFirst(string filePath,string nodeName)
        {
            List<string> strnodes = new List<string>();        
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);
            //根节点root
            XmlNode rootNode = xDoc.SelectSingleNode("root");
            string name = rootNode.Name;
            if (rootNode != null)
            {
                //获取根节点下面的子节点
                XmlNodeList nodeList = rootNode.ChildNodes;
                //遍历子节点，查找名称的子节点
                foreach (XmlNode item in nodeList)
                {
                    if (item.Name == nodeName)
                    {
                        //获取孙子节点
                        XmlNodeList subNodeList = item.ChildNodes;
                        if (subNodeList != null)
                        {
                            foreach (XmlNode xn in subNodeList)
                            {
                                XmlElement xmle = (XmlElement)xn;
                                strnodes.Add(xmle.InnerText);
                            }
                        }
                        return strnodes[0];
                    }
                }
            }
            return null;
        }
        /// <summary>
        /// 读取filePath文件的Node节点下第一个名称为attribute的数据
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <param name="attribute">属性</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [Description("读取filePath文件的Node节点下第一个名称为attribute的数据")]
        public static string ReadXml(string filePath,string nodeName, string attribute)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);
            //根节点root
            XmlNode rootNode = xDoc.SelectSingleNode("root");
            string name = rootNode.Name;
            if (rootNode != null)
            {
                //获取根节点下面的子节点
                XmlNodeList nodeList = rootNode.ChildNodes;
                //遍历子节点，查找名称的子节点
                foreach (XmlNode item in nodeList)
                {
                    if (item.Name == nodeName)
                    {
                        //获取孙子节点
                        XmlNodeList subNodeList = item.ChildNodes;
                        if (subNodeList != null)
                        {
                            foreach (XmlNode xn in subNodeList)
                            {
                                if (xn.Name == attribute)
                                {
                                    XmlElement xmle = (XmlElement)xn;
                                    return (xmle.InnerText);//相同情况下的第一个
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }
    }
}
